package org.point.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.point.common.controller.BaseController;
import org.point.common.util.DoPage;
import org.point.domain.CreditsType;
import org.point.service.CreditsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName: CreditsTypeController  
 * @Description: TODO(这里用一句话描述这个类的作用)  
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午9:18:25  
 *
 */
@Controller
@RequestMapping("/creditsT/")
public class CreditsTypeController extends BaseController {

	@Autowired
	private CreditsTypeService service;
	
	/**
	 * 
	 * @Title: creditsTypeList 
	 * @Description: TODO(查询积分规则分类) 
	 * @param @param memberId
	 * @param @param currentPage
	 * @param @param userLevelId
	 * @param @param keyWord
	 * @param @param response    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "ctList.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void creditsTypeList(@RequestParam(value = "memberId") String memberId,
			@RequestParam(value = "currentPage", required=false) Integer currentPage,
			@RequestParam(value = "userLevelId", required=false) String userLevelId, 
			@RequestParam(value = "status", required=false) String status, 
			@RequestParam(value = "keyWord", required=false) String keyWord,
			HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		DoPage<CreditsType> page = new DoPage<CreditsType>();
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (memberId != null) {
				map.put("memberId", memberId);
				page.setPageSize(10);
				if(currentPage != null){
					page.setCurrentPage(currentPage);
				}
				map.put("firstRecord", page.getFirstRecord());
				map.put("pageSize", page.getPageSize());
				
				if(strOperateUtil.isNullForString(userLevelId)){
					map.put("userLevelId", userLevelId);
				}
				if(strOperateUtil.isNullForString(keyWord)){
					map.put("keyWord", "%"+keyWord+"%");
				}
				
				page.setMap(map);
				// 查询积分规则分页数据
				DoPage<CreditsType> resultPage = service.searchCRForPage(page);
				// 返回分页参数，及积分规则分类数据
				jsonObj.put("page", resultPage);
				// 查询积分规则成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 会员单位ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", 0);
			throw new RuntimeException("查询积分规则分类错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (IOException e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}
	
	/**
	 * 
	 * @Title: insertCT 
	 * @Description: TODO(新增积分规则分类) 
	 * @param @param record    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "insertCT.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void insertCT(@ModelAttribute("CreditsType") CreditsType record, HttpServletResponse response){
		
		// 测试
		String memberId = "member001"; 
		
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		String ctCode = getNextNo("CT");
		try {
			if(record != null){
				record.setCreateDt(new Date());
				record.setCreateOper("Admin");
				record.setDelFlag(new Byte("0"));
				record.setMemberId(memberId);
				result = service.insertCT(record,ctCode);
				// 积分规则分类新增成功
				jsonObj.put("resultMsg", result);
			} else {
				// 积分规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("积分规则分类新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	/**
	 * 
	 * @Title: delCT 
	 * @Description: TODO(批量删除积分规则分类) 
	 * @param @param record    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "delCT.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void delCT(@RequestParam(value = "ids") String ids, HttpServletResponse response){
		// 测试
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		String newIDs = ids.substring(9, ids.length()-1);
		try {
			map.put("newIDs", newIDs);
			result = service.deleteCTs(map);
			//result = service.deleteCT();
			if(result>0){
				// 积分规则分类新增成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 积分规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("积分规则分类新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	/**
	 * 
	 * @Title: editCreditsType 
	 * @Description: TODO(修改积分规则分类) 
	 * @param @param record
	 * @param @param response    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "editCreditsType.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void editCreditsType(@ModelAttribute("CreditsType") CreditsType record, HttpServletResponse response){
		
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		try {
			if(record != null){
				record.setCreditsTypeId("CT00008");
				record.setCreateDt(new Date());
				record.setCreateOper("Admin");
				record.setDelFlag(new Byte("0"));
				result = service.editCreditsType(record);
				// 积分规则分类修改成功
				jsonObj.put("resultMsg", result);
			} else {
				// 积分规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("积分规则分类修改错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	
	
	
	
	/**
	 * 
	 * @Title: editForStatus 
	 * @Description: TODO(修改积分规则分类) 
	 * @param @param creditsTypeId
	 * @param @param memberId
	 * @param @param status    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "editCTStatus.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void editForStatus(@RequestParam(value = "creditsTypeId") String creditsTypeId, @RequestParam(value = "memberId") String memberId, @RequestParam(value = "status") Integer status, HttpServletResponse response){

		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;
		
		try {
			if(strOperateUtil.isNullForString(creditsTypeId)){
				map.put("creditsTypeId", creditsTypeId);
				if(strOperateUtil.isNullForString(memberId)){
					map.put("memberId", memberId);
					if(status != null){
						map.put("status", status);
						result = service.editCtStatus(map);
						// 修改状态成功
						jsonObj.put("resultMsg", result);
					} else {
						// 状态为null
						jsonObj.put("resultMsg", -3);
					}
				} else {
					// 会员单位ID为null
					jsonObj.put("resultMsg", -2);
				}
			} else {
				// 积分规则分类ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("积分规则分类修改状态错误!", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
	/**
	 * 
	 * @Title: getCreditsType 
	 * @Description: TODO(根据积分规则分类ID，查询详细信息) 
	 * @param @param creditsTypeId    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	@RequestMapping(value = "getCT.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void getCreditsType(@RequestParam(value = "id")String id, HttpServletResponse response){
		
		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;
		
		try {
			if(strOperateUtil.isNullForString(id)){
				map.put("id", Integer.parseInt(id));
				CreditsType record = service.getCTByCTID(map);
				System.out.println(record.getCreditsTypeName());
				jsonObj.put("record", record);
				// 查询积分规则详细信息，成功
				jsonObj.put("resultMsg", 1);
			}else{
				// 积分规则分类ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("查询积分规则分类详细信息！错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}
		
	}
	
}
